//
//  TimeBarView.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/19.
//
//

#include "TimeBarView.h"
#include "AttackAction.h"
#include "TimeBarModel.h"

USING_NS_CC;

#define DEF_STAR1X  (-9.5)
#define DEF_STAR2X  23
#define DEF_STAR3X  45

#define STAR3_PERCENT 50
#define STAR2_PERCENT 30


#define ACTION_X_PAD    (7)
#define ACTION_Y_PAD    (-33)



typedef enum {
    ZBg = 0,
    ZProgress,
    ZBox,
    ZStar,
    ZAction,
    

} ZIndex;

TimeBarView::TimeBarView()
:timeBar(NULL)
,maxTime(0)
,lastTime(0)
,star1(NULL)
,star2(NULL)
,star3(NULL)
,m_isPause(false)
{
}

TimeBarView* TimeBarView::create(int max)
{
    TimeBarView *pRet = new(std::nothrow) TimeBarView();
    if (pRet && pRet->init(max)){
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

bool TimeBarView::init(int max)
{
    if (!Layer::init()) {
        return false;
    }
    maxTime = max;
    lastTime = max;
    bgInit();
    progressBarInit();
    starInit();
    scheduleUpdate();
    
    
    return true;
}

void TimeBarView::update(float dt)
{
    if(m_isPause) return;
    
    if(lastTime > 0){
        lastTime -= dt;
        if(lastTime <= 0){
            _callback(this);
            lastTime = 0;
        }
        timeBar->setPercentage(lastTime*100/maxTime);
        
    }
}

void TimeBarView::starInit()
{
    star1 = Sprite::createWithSpriteFrameName("TimeStar1.png");
    star1->setAnchorPoint(Vec2::ZERO);
    star1->setPosition(Point(DEF_STAR1X,0));
    this->addChild(star1,ZStar);
    
    star2 = Sprite::createWithSpriteFrameName("TimeStar2.png");
    star2->setAnchorPoint(Vec2::ZERO);
    star2->setPosition(Point(DEF_STAR2X,0));
    this->addChild(star2,ZStar);
    
    star3 = Sprite::createWithSpriteFrameName("TimeStar3.png");
    star3->setAnchorPoint(Vec2::ZERO);
    star3->setPosition(Point(DEF_STAR3X,0));
    this->addChild(star3,ZStar);
}

void TimeBarView::progressBarInit()
{
    auto sprite = Sprite::createWithSpriteFrameName("TimeBarProgress.png");
    timeBar= ProgressTimer::create(sprite);
    timeBar->setAnchorPoint(Vec2::ZERO);
    timeBar->setPosition(Vec2::ZERO);
    addChild(timeBar,ZProgress);
    timeBar->setType(ProgressTimer::Type::BAR);
    timeBar->setPercentage(100);
    timeBar->setMidpoint(Point(0,0));
    timeBar->setBarChangeRate(Point(1,0));
}

void TimeBarView::bgInit()
{
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("TimeBar.plist");
    SpriteBatchNode *spriteSheet = SpriteBatchNode::create("TimeBar.pvr.ccz");
    addChild(spriteSheet);
    
    auto spriteBg = Sprite::createWithSpriteFrameName("TimeBarBg.png");
    spriteBg->setAnchorPoint(Vec2::ZERO);
    spriteBg->setPosition(Vec2::ZERO);
    this->addChild(spriteBg);
    
    auto spriteBox = Sprite::createWithSpriteFrameName("TimeBarBox.png");
    spriteBox->setAnchorPoint(Vec2::ZERO);
    spriteBox->setPosition(Vec2::ZERO);
    this->addChild(spriteBox,ZBox);
}

void TimeBarView::setCallback(const timeBarViewCallback& callback)
{
    _callback = callback;
}

void TimeBarView::setPause(bool pause)
{
    m_isPause = pause;
}

void TimeBarView::changeTime(float time)
{
    lastTime += time;
    if(lastTime > maxTime){
        lastTime = maxTime;
    }else if(lastTime < 0){
        lastTime = 0;
    }
    timeBar->setPercentage(lastTime*100/maxTime);

    showAction();
}

void TimeBarView::showAction()
{
    auto attack = AttackAction::create(HealthAttack);
    attack->setPosition(ACTION_X_PAD,ACTION_Y_PAD);
    addChild(attack,ZAction);
}

void TimeBarView::recordTime()
{
    if(timeBar->getPercentage() > STAR3_PERCENT){
        TimeBarModel::getInstance()->setStarNum(3);
    }else if(timeBar->getPercentage() > STAR2_PERCENT){
        TimeBarModel::getInstance()->setStarNum(2);
    }else{
        TimeBarModel::getInstance()->setStarNum(1);
    }
}
